﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public enum RET_CODE
    {
        RET_SUC = 0,

        RET_NOLOGIN = -1000,
        RET_NORECORD = -1001,

        RET_INPUT_NOTIME = -100,
        RET_INPUT_ERRTIME = -101,
        RET_INPUT_INVALIDTIME = -102,
        RET_INPUT_TIMEBEFORE_START = -103,

        RET_INPUT_SAMERECORD = -200,

        RET_INPUT_NOWEIGHT = -300,

        RET_NOHEIGHT = -400,

        RET_ERROR = -1,

        //圈子成员邀请
        RET_ISINVITED = -500,
        RET_ISGROUPMEMBER = -600,

        //指标
        RET_INVALIDVALUE = -700

    }

    public abstract class XmlBasePage : System.Web.UI.Page
    {
        protected PatientRecordInfo PrData;
        protected bool _bCheckLogin = true ;

        #region 用户基本信息
        protected UserInfo UserData = new UserInfo();// 当前用户的信息
        protected UserGroupInfo UserGroupInfo = new UserGroupInfo(); //当前用户组信息   
        #endregion

        virtual protected void ResponseContext(RET_CODE rCode, string sMsg)
        {
            //生产xml文档
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            Response.ContentType = "text/xml";
            using (XmlWriter wr = XmlWriter.Create(Response.OutputStream, settings))
            {
                // Write XML data.
                wr.WriteStartElement("result");
                wr.WriteAttributeString("code", ((int)rCode).ToString());
                wr.WriteAttributeString("msg", sMsg);                    
                wr.WriteEndElement();
                wr.Flush();
            }
        }

        virtual protected bool CheckLogin()
        {
            UserData.Id = CookiesTools.GetUserIdCookie();
            if (UserData.Id > 0) //表示永久登陆的用户
            {
                UserData.UserName = CookiesTools.GetUserNameCookie();   //直接从cookie中获取用户名
                if (UserData.UserName.Length <= 0)
                {
                    UserData.Id = -1;    //如果不存在用户名，则为无效的cookie
                    return false;
                }
                PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
                return true;
            }
            return false;
            
        }

        abstract protected RET_CODE CheckParam(ref string sMsg);
        abstract protected void Handle();
        protected RET_CODE CheckParamDate(string dateName, DateTime minDate, DateTime maxDate, out DateTime dateValue, out string sMsg)
        {
            string sDate = RequestTools.GetString(dateName);
            dateValue = DateTime.Today;
            sMsg = "";
            if (sDate == "")
            {
                sMsg = "请填写日期！";
                return RET_CODE.RET_INPUT_NOTIME ;
            }
           
            if (!DateTime.TryParse(sDate, out dateValue))
            {
                sMsg = "错误的日期：" + sDate;
                return RET_CODE.RET_INPUT_NOTIME;
            }

            if (dateValue > maxDate)
            {
                sMsg = string.Format("日期不能晚于：{0:yyyy-MM-dd}", maxDate);
                return RET_CODE.RET_INPUT_INVALIDTIME;
            }

            if (dateValue < minDate )
            {
                sMsg = string.Format("日期不能早于：{0:yyyy-MM-dd}", minDate);
                return RET_CODE.RET_INPUT_INVALIDTIME;
            }

            return RET_CODE.RET_SUC;

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            string sErrMsg = "";
            RET_CODE ret = RET_CODE.RET_SUC;
            if ( RET_CODE.RET_SUC != ( ret = CheckParam(ref sErrMsg) ))
            {
                ResponseContext(ret, sErrMsg);
                return;
            }
            if ( _bCheckLogin && !CheckLogin())
            {
                ResponseContext(RET_CODE.RET_NOLOGIN, "你还没有登陆");
                return;
            }
            Handle();
        }
    }
}
